package Exercise;
//通过同步代码块解决接口线程安全问题
public class windowTest1 {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread);
        Thread t2 = new Thread(myThread);
        Thread t3 = new Thread(myThread);
        Thread t4 = new Thread(myThread);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t4.setName("窗口4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }


}

class MyThread implements Runnable {
    //实现类的方式来创建多线程更适用于有共享数据的
    private int tickte = 1000;
    Object obj = new Object();


    public void run() {
//        long startTime = System.currentTimeMillis();
        while (true) {
            synchronized(obj) { //obj==this; this代表MyThread的对象myThread
                if (tickte > 0) {
//                    try {
//                        Thread.sleep(1);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println(Thread.currentThread().getName() + ":" + tickte);
                    tickte--;
                } else {
                    break;
                }
            }
        }
//        long latterTime = System.currentTimeMillis();
//        System.out.println((startTime-latterTime)/1000+"s");
    }


}
